<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container" :inline="true"  label-width="120px" >
      <div class="createPost-main-container">
        <el-row class="hidden-columns">
          <el-col :span="12" >
            <el-form-item  label="ID:"   label-width="120px"  :required="true"  prop="id" >
              <el-input disabled  v-model="editForm.id"  :maxlength="20"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
            <el-form-item  label="一级仓库名称:"   label-width="120px"  :required="true"  prop="name" >
              <el-input  v-model="editForm.storageName"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>
        <!--<el-row>
          <el-col :span="12" >
            <el-form-item  label="一级仓库简称:" label-width="120px"  prop="shortName"  >
              <el-input v-model="editForm.shortName"  :maxlength="11" required />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人姓名:" label-width="120px"     prop="contactName">
              <el-input v-model="editForm.contactName" :maxlength="10"  required  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item  label="联系人电话:" prop="contactPhone" >
              <el-input v-model="editForm.contactPhone" :maxlength="10"   />
            </el-form-item>
          </el-col>
        </el-row>-->

        <el-row>
          <el-col :span="12"  >
            <el-form-item  label="概述:" prop="desc"  >
              <el-input  v-model="editForm.storageDesc" type="textarea" :maxlength="200" name="desc"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" style="padding-left:60px;">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="submitForm">提  交</el-button>
            <el-button  type="primary" size="small" icon="el-icon-lock" class="error" @click="resetForm">重  置</el-button>
          </el-col>


        </el-row>
      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { validateURL } from '@/utils/validate'
import { createStorage,fetchStorage , updateStorage } from '@/api/storage'
import { userSearch } from '@/api/remoteSearch'
import Warning from './Warning'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'

const defaultForm = {
  id: '0',
  name: '',
  shortName: '',
  contactName: '',
  contactPhone: '',
  desc: '',
  status: 1 ,

}

export default {
  name: 'StorageEdit',
  components: { Tinymce, MDinput},
  props: {
    // isEdit: {
    //   type: Boolean,
    //   default: false
    // }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        if(rule.field == ''){
          callback(new Error('名称为必传项！'))
        }

      } else {
        callback()
      }
    }

    return {
      postForm: Object.assign({}, defaultForm),
      editForm:{},
      loading: false,
      userListOptions: [],
      rules: {
        name: [{ validator: validateRequire }],

      },
      isEdit: 1,
    }
  },
  computed: {
    contentShortLength() {

    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.query && this.$route.query.id
      console.log( this.$route.query  )
      this.fetchData(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },
  methods: {
    fetchData(id) {

      fetchStorage({id: id} ).then(response => {
        this.editForm = response.data.data
        console.log(response.data)
        //this.editForm.title += `   Article Id:${this.postForm.id}`
      }).catch(err => {
        console.log(err)
      })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      //this.postForm.display_time = parseInt(this.display_time / 1000)
      this.postForm.id =  this.editForm.id
      this.postForm.name = this.editForm.storageName
      this.postForm.shortName = this.editForm.shortName
      this.postForm.contactName = this.editForm.contactName
      this.postForm.contactPhone = this.editForm.contactPhone
      this.postForm.desc = this.editForm.storageDesc
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.doSubmitForm()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    doSubmitForm() {
      updateStorage(this.postForm).then(response => {
        if(response.data.code == 0){
          this.$message({
            message:  '保存成功!',
            type: 'success'
          })
          this.resetForm()
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        // this.list = response.data.items
        // this.total = response.data.total
        this.listLoading = false
      })
    },

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
